//
//  YHKOriginalCell.swift
//  xinlangweiBo2
//
//  Created by 杨和逵 on 16/3/1.
//  Copyright © 2016年 yanghekui. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit


/**
  源创微博
     头像
     名称
     认证
     vip
     时间
     来源
     内容
    
 
 
 
 */



///边距
let margin = 10

class YHKOriginalCell: UIView {

  //记录下约束图片底部约束
    var constant: Constraint?
    
    
    
    //定义一个属性来接收模型里的数据
    var homeCellViewModel:YHKHomeCellViewModel?{
        
        didSet{
            
            
            
          
            //头像
            let url = NSURL(string: homeCellViewModel?.statrsModel?.user?.profile_image_url ?? "")
        
            if let Ourl = url{
                iconImageView.sd_setImageWithURL(Ourl)
            }
            //名字
          nameLabel.text = homeCellViewModel?.statrsModel?.user?.screen_name 
            
            //认证
            approveImageView.image = homeCellViewModel?.verified_image
            
            //vip
            vipImageView.image = homeCellViewModel?.mbrank_image
            
            //时间
//            timeLabel.text = homeCellViewModel?.time_String
            timeLabel.text = homeCellViewModel?.dealCreateDate(homeCellViewModel?.statrsModel?.created_at)
            
            //来源
            sourceLabel.text = homeCellViewModel?.dealSourec(homeCellViewModel?.statrsModel?.source)
            
            //内容
            contentLabel.text = homeCellViewModel?.statrsModel?.text
            
            
            ///显示不显示配图，当有配图的时候，需要配图，改变约束，没有的时候就隐藏，修改约束
            //发表的图片
           
            //cell重用了，导致约束加重，要清空约束
            constant?.uninstall()
            
            
            if let pic_urls = homeCellViewModel?.statrsModel?.pic_urls where pic_urls.count > 0 {
                
                pictureView.hidden = false
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.constant = make.bottom.equalTo(pictureView.snp_bottom).offset(margin).constraint
                    
                    //传递数据给collectionView这个类的成员属性
//                    pictureView.homeCellViewModel = homeCellViewModel
                    
                    
                    //还需要传递转发微博的图片数据，所用换个方法传递数据
                    var result = [YHKPictureViewModel]()
                    
                    for model in pic_urls{
                        
                        let viewModel = YHKPictureViewModel()
                        
                        viewModel.model = model
                        
                        result.append(viewModel)
                    }
                    
                    pictureView.pictureList = result
                    
                })
                
            }else{

                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //上
                  self.constant = make.bottom.equalTo(contentLabel.snp_bottom).offset(margin).constraint
                })
            }
            
            
        }
    }
    

    //创建视图三部曲
    
    //第一步init
    override init(frame:CGRect) {
        super.init(frame: frame)
        
        setUpView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //第二部，创建一个私有方法，添加控件和设置约束
   private func setUpView (){
    
    //添加控件
    addSubview(iconImageView)
    addSubview(nameLabel)
    addSubview(approveImageView)
    addSubview(vipImageView)
    addSubview(timeLabel)
    addSubview(sourceLabel)
    addSubview(contentLabel)
    addSubview(pictureView)//添加上传图片的视图
    
    
    
    //设置约束
//    头像--->上 左 大小
    iconImageView.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(self.snp_top).offset(margin)
        make.left.equalTo(self.snp_left).offset(margin)
        make.size.equalTo(CGSizeMake(40, 40))
    }
    
//    名称
    nameLabel.snp_makeConstraints { (make) -> Void in
        //左 上
        make.left.equalTo(iconImageView.snp_right).offset(margin)
        make.top.equalTo(iconImageView.snp_top)
    }
    
//    认证
    approveImageView.snp_makeConstraints { (make) -> Void in
        //头像的左边X 底部Y
        make.centerX.equalTo(iconImageView.snp_right)
        make.centerY.equalTo(iconImageView.snp_bottom)
    }
    
//    vip
    vipImageView.snp_makeConstraints { (make) -> Void in
        //左边 名字，名字的中心Y对其
        make.left.equalTo(nameLabel.snp_right).offset(margin)
        make.centerY.equalTo(nameLabel.snp_centerY)
    }
    
//    时间
    timeLabel.snp_makeConstraints { (make) -> Void in
        //左 到 头像 10. 上到名字5
        make.left.equalTo(iconImageView.snp_right).offset(margin)
        make.top.equalTo(nameLabel.snp_bottom).offset(margin / 2)
    }
    
//    来源
    sourceLabel.snp_makeConstraints { (make) -> Void in
        //左到时间10，上到名字5
        make.left.equalTo(timeLabel.snp_right).offset(margin)
        make.top.equalTo(nameLabel.snp_bottom).offset(margin / 2)
    }
    
//    内容
    contentLabel.snp_makeConstraints { (make) -> Void in
        //左 右 上 下(可以不用设置下)
        make.left.equalTo(self.snp_left).offset(margin)
        make.right.equalTo(self.snp_right)
        make.top.equalTo(iconImageView.snp_bottom).offset(margin)
//        make.bottom.equalTo(self.snp_bottom).offset(margin)
    }
    
    
    //图片
    pictureView.snp_makeConstraints { (make) -> Void in
        //上 左 宽高
        make.top.equalTo(contentLabel.snp_bottom).offset(margin)
        make.left.equalTo(self.snp_left).offset(margin)
        make.size.equalTo(CGSizeMake(100, 100))
        
        
    }
    
    //告诉视图底部
    self.snp_makeConstraints { (make) -> Void in
        
      constant = make.bottom.equalTo(pictureView.snp_bottom).offset(margin).constraint
    }
    
    }
    
//MARK:懒加载
//    头像
   private lazy var iconImageView:UIImageView = {
    
    //创建
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
    
        icon.layer.cornerRadius = icon.bounds.size.width / 4
    
         icon.layer.masksToBounds = true
    
    
         return icon
    }()
    
//    名称
   private lazy var nameLabel:UILabel = {
    
    //创建
        let name = UILabel()
        
        name.text = "微博名字"
        
        name.font = UIFont.systemFontOfSize(15)
    
        
        return name
    }()
    
//    认证
   private lazy var approveImageView:UIImageView = {
    
    //创建
       let approve = UIImageView(image: UIImage(named: "avatar_vip"))
    
    return approve
        
    }()
    
//    vip
   private lazy var vipImageView:UIImageView = {
    
    //创建
        let vip = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    
        return vip
    }()
    
//    时间
   private lazy var timeLabel:UILabel = {
    
    let time = UILabel()
        
        time.font = UIFont.systemFontOfSize(13)
        
        time.text = "时间"
        
        time.textColor = UIColor.orangeColor()
        
        return time
        
    }()
    
//    来源
   private lazy var sourceLabel:UILabel = {
    
    let source = UILabel()
        
        source.text = "微博来源"
        
        source.font = UIFont.systemFontOfSize(13)
        
        return source
    
    
    }()
    
//    内容
   private lazy var contentLabel:UILabel = {
    
    let content = UILabel()
        
        content.text = "微博内容"
        
        content.font = UIFont.systemFontOfSize(15)
        
        content.numberOfLines = 0
        
        return content
    
    
    }()
    
//    发表的图片
    private lazy var pictureView:YHKPictureView = YHKPictureView()
    
    
}
